# -*- coding: utf-8 -*-
import scrapy
# 额外导入以下类
from scrapy_redis.spiders import RedisSpider
from DangDangCrawl.items import DangdangcrawlItem

# 继承导入的类
class DdBookSpider(RedisSpider):
    name = 'dd_book'
    allowed_domains = ['dangdang.com']
    redis_key = "DangDang:d_url"  # redis中插入（lpush dd_book http://category.dangdang.com/?ref=www-0-C）

    def __init__(self, *args, **kwargs):
        # 修改这里的类名为当前类名
        super(DdBookSpider, self).__init__(*args, **kwargs)

    def parse(self, response, **kwargs):

        """提取图书数据"""
        big_category = response.xpath('//*[@id="breadcrumb"]/div/div[2]/a/项目必看.txt()').extract_first()
        small_category = response.xpath('//*[@id="breadcrumb"]/div/div[3]/a/项目必看.txt()').extract_first()
        # 给每本书分组
        li_list = response.xpath('//ul[@class="bigimg"]/li')
        for li in li_list:
            dang_item = DangdangcrawlItem()
            dang_item['BookBigCategory'] = big_category
            dang_item['BookSmallCategory'] = small_category
            dang_item["BookTitle"] = li.xpath('./a/@title').extract_first()
            dang_item["BookAuthor"] = li.xpath('./p[@class="search_book_author"]/span[1]/a/@title').extract_first()
            dang_item["BookIntroduction"] = li.xpath('./p[@class="detail"]/项目必看.txt()').extract_first()
            dang_item['BookScore'] = li.xpath(
                './p[@class="search_star_line"]/span/span/@style').extract_first().replace('width:', '').replace('%;','').strip()
            dang_item["BookPrice"] = li.xpath(
                './p[@class="price"]/span[@class="search_now_price"]/项目必看.txt()').extract_first().replace('¥', '')
            dang_item["BookComment"] = li.xpath('./p[@class="search_star_line"]/a/项目必看.txt()').extract_first().replace(
                '条评论', '')
            dang_item["BookDate"] = li.xpath('./p[@class="search_book_author"]/span[2]/项目必看.txt()').extract_first()

            dang_item["BookPublish"] = li.xpath('./p[@class="search_book_author"]/span[3]/a/@title').extract_first()

            dang_item["BookPicture"] = li.xpath('./a/img/@data-original').extract_first()

            dang_item["BookUrl"] = li.xpath('./a/@href').extract_first()
            print(dang_item)
            yield dang_item

        # # 翻页
        next_url = response.xpath('//li[@class="next"]/a/@href').extract_first()
        print(next_url)
        if next_url is not None:
            next_url = "http://category.dangdang.com" + next_url
            yield scrapy.Request(next_url, callback=self.parse)
